import { getAccessToken, removeAccessToken, setAccessToken } from '@/utils/accessToken';

import { setting } from '@/config/setting';
const { title, tokenName } = setting;
import { resetRouter } from '@/router';

import i18n from '@/locales';
const { global } = i18n;

import { ElMessage, ElNotification } from 'element-plus';

const state = {
  accessToken: getAccessToken(),
  username: '',
  avatar: '',
  permissions: [],
};

const getters = {
  accessToken: (state) => state.accessToken,
  username: (state) => state.username,
  avatar: (state) => state.avatar,
  permissions: (state) => state.permissions,
};
const mutations = {
  setAccessToken(state, accessToken) {
    state.accessToken = accessToken;
    console.log(state.accessToken);
    setAccessToken(accessToken);
  },
  setUsername(state, username) {
    state.username = username;
  },
  setAvatar(state, avatar) {
    state.avatar = avatar;
  },
  setPermissions(state, permissions) {
    state.permissions = permissions;
  },
};
const actions = {
  setPermissions({ commit }, permissions) {
    commit('setPermissions', permissions);
  },
  async setToken({ commit }, token) {
    console.log(token);
    commit('setAccessToken', token);
  },
  setUserInfo({ commit }, user) {},
  // async getUserInfo({ commit, state }) {
  //   const { data } = await getUserInfo(state.accessToken);
  //   if (!data) {
  //     ElMessage.error('验证失败，请重新登录...');
  //     return false;
  //   }
  //   let { permissions, username, avatar } = data;
  //   if (permissions && username && Array.isArray(permissions)) {
  //     commit('setPermissions', permissions);
  //     commit('setUsername', username);
  //     commit('setAvatar', avatar);
  //     return permissions;
  //   } else {
  //     ElMessage.error('用户信息接口异常');
  //     return false;
  //   }
  // },
  async logout({ dispatch }) {
    await dispatch('resetAccessToken');
    await resetRouter();
  },
  resetAccessToken({ commit }) {
    commit('setPermissions', []);
    commit('setAccessToken', '');
    removeAccessToken();
  },
};
export default { state, getters, mutations, actions };
